//
//  DComputeViewController.m
//  dance
//
//  Created by aether on 2023/10/19.
//

#import "DComputeViewController.h"
#import "DMetabolismViewController.h"
#import "DPressureViewController.h"
#import "DDonateViewController.h"
#import "DComputeItem.h"

@interface DComputeViewController ()

@property (nonatomic, strong) IBOutlet UIView *content;
@property (nonatomic, strong) NSArray *infos;

@end

@implementation DComputeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSLog(@"计算");
    [self initValues];
    [self initViews];
}

- (void) initValues {
    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"compute" ofType:@"json"]];
    NSError *error;
    self.infos = (NSArray *)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
}

- (void) initViews {
    self.title = @"计算";
    
    NSInteger itemCount = 2;
    CGFloat itemSpace = 10;
    CGFloat itemWidth = ([UIScreen mainScreen].bounds.size.width - (itemCount + 1) * itemSpace) / itemCount;
    CGFloat itemHeight = 175;
    
    for (int i = 0; i < self.infos.count; i++) {
        int x = i % itemCount;
        int y = i / itemCount;
        
        DComputeItem *item = [[DComputeItem alloc] initWithFrame:CGRectMake(itemSpace + (itemWidth + itemSpace) * x, itemSpace + (itemHeight + itemSpace) * y, itemWidth, itemHeight) withDic:[self.infos objectAtIndex:i]];
        [item setTag:i];
        [item addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemTap:)]];
        [self.content addSubview:item];
    }
    
    
    UIBarButtonItem *customButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"来源" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonTapped:)];
    self.navigationItem.rightBarButtonItem = customButtonItem;

}

#pragma mark - 事件

- (void) rightButtonTapped:(UIBarButtonItem *) item {
    NSURL *url = [NSURL URLWithString:@"https://m.youlai.cn/"];
    [[UIApplication sharedApplication] openURL:url];

}

- (void) itemTap:(UITapGestureRecognizer *)gesture {
    if (gesture.view == nil) {
        return;
    }
    
    int tag = (int)gesture.view.tag;
    if (tag == 0 || tag == 1 || tag == 3) {
        DMetabolismViewController *dinfo = [[DMetabolismViewController alloc] initWithNibName:@"DMetabolismViewController" bundle:nil];
        dinfo.title = [[self.infos objectAtIndex:tag] objectForKey:@"name"];
        dinfo.type = tag;
        [self.navigationController pushViewController:dinfo animated:YES];
    } else if (tag == 4) {
        DPressureViewController *dinfo = [[DPressureViewController alloc] initWithNibName:@"DPressureViewController" bundle:nil];
        dinfo.title = [[self.infos objectAtIndex:tag] objectForKey:@"name"];
        [self.navigationController pushViewController:dinfo animated:YES];
    } else if (tag == 2) {
        DDonateViewController *dinfo = [[DDonateViewController alloc] initWithNibName:@"DDonateViewController" bundle:nil];
        dinfo.title = [[self.infos objectAtIndex:tag] objectForKey:@"name"];
        [self.navigationController pushViewController:dinfo animated:YES];
    }
    
//    DDonateViewController *dinfo = [[DDonateViewController alloc] initWithNibName:@"DDonateViewController" bundle:nil];
//    dinfo.title = [[self.infos objectAtIndex:0] objectForKey:@"name"];
//    [self.navigationController pushViewController:dinfo animated:YES];
}


@end
